package com.qianfeng.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class LoginController {

    @RequestMapping(value = "/login")
    public String login(String email, String empPassword, Model model) {

        //获取当前用户实体
        Subject subject = SecurityUtils.getSubject();

        //将前端发送过来的用户名和密码封装进token中
        UsernamePasswordToken token = new UsernamePasswordToken(email, empPassword);
        //记住我

        //调用Shiro提供的方法进行验证
        try {
            subject.login(token);
        } catch (UnknownAccountException uae) {
            model.addAttribute("msg", "用户不存在");
            System.out.println("用户名不存在:" + uae.getMessage());
            return "login";
        } catch (IncorrectCredentialsException ice) {
            System.out.println("密码错误:" + ice.getMessage());
            model.addAttribute("msg", "密码不存在");
            return "login";
        } catch (AuthenticationException ae) {
            System.out.println("其他异常:" + ae.getMessage());
            model.addAttribute("msg", "其他");
            return "login";
        }
        return "home";
    }

}
